<?php
/**
 *
 * (model)
 */
namespace App\Models\Product;

use App\Models\Basics\SysCountry;
use Illuminate\Database\Eloquent\Model;
class ProductAmazonSkuCCS extends Model
{

    protected $table = 'product_amazon_sku_ccs';

    protected $guarded = [];

    public function classify(){
        return $this->belongsTo(ProductClassify::class,'classify_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public static function getSellerId($countryId,$classifyId){
         $sellerId = self::query()
            ->where('country_id',$countryId)
            ->where('classify_id',$classifyId)
            ->value('seller_id');
        return $sellerId ?: 0;
    }

    public static function upSellerId($countryId,$classifyId,$sellerId){
        return ProductAmazonSku::query()
            ->whereHas('sku',function ($query) use ($classifyId){
                $query->where('classify_id',$classifyId);
            })
            ->where('country_id',$countryId)
            ->where(function ($query) use ($sellerId){
                $query->whereNull('seller_id')
                    ->orWhere('seller_id','<>',$sellerId);
            })
            ->update(['seller_id' => $sellerId]);
    }

    //根据品类+国家获取对应销售
    public static function returnSellerId($countryId,$classifyId,$sellerId){
        return ProductAmazonSkuCCS::query()->firstOrCreate([
            'country_id' => $countryId,
            'classify_id' => $classifyId,
        ],['seller_id' => $sellerId]);
    }
}
